/*
 * This class controls the cart for the payment.
 * This class is saved in a session.
 */
package classes;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import models.Course;

/**
 *
 * @author Achie
 */
public class Cart {

    private List<Course> cart = new ArrayList<Course>();

    public Cart() {
    }

    public void addToCart(Course course) { //add course to cart        
        if (isNoDuplicate(course)) {
            cart.add(course);
        }
    }

    public void setCart(List<Course> cart) { //set the cartlist
        this.cart = cart;
    }

    public List<Course> getCart() { //get the cartlist
        return cart;
    }

    public boolean isNoDuplicate(Course course) { //checks whether the course that is inserted is already in the cart
        for (int i = 0; i < cart.size(); i++) {
            if (cart.get(i).getId() == course.getId()) {
                cart.remove(i);
                return false;
            }
        }
        return true;
    }

    public void removeFromCart(Course course) { //remove course from cart
        cart.remove(course);

    }

    public int getAmount() { //get the amount of courses currently in the cart
        return cart.size();
    }

    public double getTotalPrice() { //get the total price of all courses in the cart
        double sum = 0;
        for (int i = 0; i < cart.size(); i++) {
            sum = sum + cart.get(i).getPrice();
        }
        return sum;
    }
}
